﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Classes;

public partial class Cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }

    private void PopulateControls()
    {
        List<Product> items = ShoppingCart.Items();
        if (items.Count == 0)
        {
            // neu so product = 0 thi hien thi Empty 
            lblTitle.Text = "Shopping Cart is Empty";

            // con lai an het di 
            grid.Visible = false;
            phToolbar.Visible = false;
        }
        else
        {
            grid.DataSource = items;
            grid.DataBind();
            // Hien thi total co them dau $ 
            lblTotal.Text = ShoppingCart.TotalAmount().ToString("c");
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnUpdate.Click += new EventHandler(btnUpdate_Click);
    }

    void btnUpdate_Click(object sender, EventArgs e)
    {
        // lay tong so hang trong grid 
        int rowsCount = grid.Rows.Count;
        GridViewRow row;

        // textbox nhap so luong 
        TextBox txtQuantity;

        // checkbox danh dau product can xoa 
        CheckBox chbRemove;

        // id cua hang hien tai 
        string productId;
        int quantity;
        bool success = true;
        for (int i = 0; i < rowsCount; i++)
        {
            row = grid.Rows[i];
            // lay productid cua hang hien tai 
            productId = grid.DataKeys[i].Value.ToString();
            // tim textbox nhap so luong 
            txtQuantity = (TextBox)row.FindControl("txtQuantity");
            // tim checkbox remove 
            chbRemove = (CheckBox)row.FindControl("chbRemove");

            // neu checkbox da check ==> can xoa product nay 
            if (chbRemove.Checked)
            {
                // tinh trang cap nhat gio hang ( thanh cong hay that bai) 
                // do dung toan tu && nen chi can 1 cai fail ==> success luon luon false 
                success = success && ShoppingCart.Remove(productId);
            }
            // kiem tra so luong nhap co hop le hay khong 
            else if (int.TryParse(txtQuantity.Text, out quantity))
            {
                // neu nhap hop le thi cap nhat so luon 
                success = success && ShoppingCart.Update(productId, quantity);
            }
            else
            {
                success = false;
            }

            // Hien thi thong bao 
            // Khong phai tat ca viec cap nhat/xoa deu thanh cong 
            lblStatus.Text = success ?
                    "Your shopping cart was successfully updated!" :
                    "Some quantity updates or item removes failed! Please verify your cart!";
        }
        PopulateControls();
    }
}